package text111;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import javax.management.Query;

public class Solution10 {
	public int[] levelOrder(TreeNode root) {
		//创建队列
		Queue<TreeNode> queue = new LinkedList();
		//创建动态数组
		ArrayList<Integer> res = new ArrayList<Integer>();
		//要判断根节点是否为空值
		if (root==null) {
			int a[] = new int[0];
			return a;
		}
		//根左右依次入队
		queue.add(root);
		while(!queue.isEmpty()) {
			//备份
			TreeNode tmpNode = queue.poll();
			res.add(tmpNode.val);
			if (tmpNode.left!=null) {queue.add(tmpNode.left);}
			if (tmpNode.right!=null) {queue.add(tmpNode.right);	}
		}
		int val[] = new int[res.size()];
		for (int i = 0; i < val.length; i++) {
			val[i] = res.get(i);
		}
		return val;
		
	

    }
}
